#pragma once
#include "base_interface.h"
#include "can_interface.h"
#include "modbus_interface.h"
#include "io_sturct.h"
#include <rclcpp/rclcpp.hpp>
#include <memory>

#define MODBUS_TYPE "modbus"
#define CANOPEN_TYPE "canopen"
namespace io_module {
class IoFactory
{
public:
    IoFactory() = default;
    explicit IoFactory(std::shared_ptr<rclcpp::Node> node);

    ~IoFactory();

    bool init(IoGroup ioGroup);
    bool factory_fun(ModuleParam type);
    bool read_input_ios(const int &count_io, uint64_t &rets);
    bool set_output_io(const int &index, const bool &level);
    bool read_analog_inputs(const int &channel_count, std::vector<BaseInterface::WORD> &rets);
    bool set_analog_output(const int &channel_index, const BaseInterface::WORD & raw_data);

private:
    void clear();
    bool sort_by_module_number();
    bool init_all_module();
    
    std::vector<BaseInterface*> m_vecInterface;
    IoGroup m_ioGroup;
    std::mutex m_mutex;
    std::mutex m_mutexA;
    bool m_init_ok;

    std::shared_ptr<rclcpp::Node> node_;
};
}